package edu.dqsy.mzq_translate;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import edu.dqsy.mzq_translate.helper.ContactOpenHelper;

public class zhuce extends AppCompatActivity {
    private Button zhuce;
    private EditText username;
    private EditText password;
    private EditText repassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);


        final ContactOpenHelper helper = new ContactOpenHelper(zhuce.this,"userinfo.db",null,1);

        helper.getWritableDatabase();


        zhuce = findViewById(R.id.zhucenew);
        username = findViewById(R.id.zhuceuser);
        password = findViewById(R.id.zhucepassworde1);
        repassword = findViewById(R.id.zhucepassword2);



        Button clean = findViewById(R.id.clean);
        clean.setOnClickListener(view -> {
            SQLiteDatabase db = helper.getWritableDatabase();
            db.execSQL("delete from userInfo");
//            db.execSQL("drop table userinfo.db");
//            "select name from sqlite_master where type='table' order by name"
        });



        zhuce.setOnClickListener(view -> {
            AlertDialog.Builder builder  = new AlertDialog.Builder(zhuce.this);
            SQLiteDatabase db = helper.getWritableDatabase();
            String us = username.getText().toString();
            String pa = password.getText().toString();
            String rpa = repassword.getText().toString();
            String s = "";
            if(us.equals(s)||pa.equals(s)||rpa.equals(s)){
                builder.setTitle("注册失败" ) ;
                builder.setPositiveButton("是" ,  null );
                builder.setMessage("请输入应户名或密码" ) ;
                builder.show();
            }else
            if (!pa.equals(rpa)){

                builder.setTitle("注册失败" ) ;
                builder.setPositiveButton("是" ,  null );
                builder.setMessage("俩次输入密码不相同" ) ;
                builder.show();
            }else{
                Cursor c = db.rawQuery("SELECT * FROM userInfo where username=?",new String[]{us});
                if (c.getCount() != 0){
                    builder.setTitle("注册失败" ) ;
                    builder.setPositiveButton("是" ,  null );
                    builder.setMessage("用户名已被注册，请重新注册" ) ;
                    builder.show();
                }else {
                    db.execSQL("Insert into userInfo (username,password) values(?,?)",new String[]{us,pa});
                    String Sql = "create table "+us+"(_id integer primary key autoincrement,name varchar(20),info varchar(20))";
                    db.execSQL(Sql);
                    db.close();
                    builder.setTitle("注册成功" ) ;
                    builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent ii = new Intent(zhuce.this, denglu.class);
                            startActivity(ii);
                        }
                    });
                    builder.setMessage("恭喜你注册成功" ) ;
                    builder.show();

                }
            }

        });

    }
}